/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.biblioteca.Dao;

import br.com.biblioteca.Bean.Cadastro;
import br.com.biblioteca.conection.CriaConexao;
import java.sql.Connection;
import java.sql.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Maicon
 */
public class CadastroDao {

    String SQL;
    private static Connection conection;

    public void Adiciona(Cadastro cad) throws ClassNotFoundException, SQLException {
        this.conection = CriaConexao.getConexao();

        SQL = "insert into usuario(Nome_Usuario) values (?)";
        try {
            try (PreparedStatement stmt = conection.prepareStatement(SQL)) {
                stmt.setString(1, cad.getNome());
                stmt.execute();
            }
        } catch (SQLException ex) {
            Logger.getLogger(CadastroDao.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public List<Cadastro> getLista() throws SQLException, ClassNotFoundException {
        this.conection = CriaConexao.getConexao();
        String select = "Select * from usuario";
        PreparedStatement stmt = CadastroDao.conection.prepareStatement(select);
        ResultSet rs = stmt.executeQuery();
        List<Cadastro> MinhaLista = new ArrayList<Cadastro>();
        while (rs.next()) {
            Cadastro c = new Cadastro();
            c.setNome(rs.getString("Nome_Usuario"));
            MinhaLista.add(c);
        }
        rs.close();
        stmt.close();
        return MinhaLista;
    }

}
